package wang.wangby.lazydoc.model;

import java.util.Date;

import wang.wangby.annotation.Param;
import wang.wangby.annotation.Property;
import wang.wangby.utils.ClassUtil;
import wang.wangby.utils.StringUtil;

public class InputParam {

	private ApiParameter param;
	private Property property;

	public InputParam(ApiParameter param, Property property) {
		this.param = param;
		this.property=property;
	}

	public String getValidate() {
		// 日期判断没实现
		return this.getPlaceholder().replace("required", "notNull");
	}
	

	public boolean isNotNull() {
		Param config=param.getConfig();
		if(config==null) {
			return false;
		}
		if(config.notNull()) {
			return true;
		}
		for(String s:config.notNulls()) {
			if(param.getInputName().equals(s)) {
				return true;
			}
		}
		return false;
	}

	public String getPlaceholder() {
		String text = "";
		if (isNotNull()) {
			text += "required,";
		}
		if (ClassUtil.isNumber(param.getType()))
			text += "number,";
		if (ClassUtil.isInstance(param.getType(), Date.class)) {
			if(property!=null&&property.dateOnly()) {
				text += "date,";
			}else {
				text += "datetime,";
			}
			
		}
		if (StringUtil.isEmpty(text)) {
			return "";
		}
		return text.substring(0, text.length() - 1);
	}

	// 根据参数创建一个空对象,构造js参数的时候用到
	public Object newInstance() {
		return null;
	}

	public String getInputName() {
		return param.getInputName();
	}
	
	public String getDescription() {
		return param.getDescription();
	}
}
